package com.sr.examination.exam.common.widget;

import android.app.Activity;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.PopupWindow;

import com.sr.examination.R;
import com.tl.commonlibrary.ui.beans.ExerciseBean;


/**
 * 弹窗
 */

public class ExamMorePop extends PopupWindow implements View.OnClickListener {
    private Activity mContext;

    public ExamMorePop(Activity context) {
        this.mContext = context;
        init();
    }

    View rootView;
    private Button collectionBtn;

    private void init() {
        setAnimationStyle(R.style.popup_show_Animation);
        // 设置弹出窗体的宽和高
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
        // 设置弹出窗体可点击
        setFocusable(true);
        setBackgroundDrawable(new ColorDrawable(0x00000000));
        //解决华为手机window被navigationBar挡住
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        rootView = View.inflate(mContext, R.layout.pop_exam_more, null);
        collectionBtn = rootView.findViewById(R.id.collectionBtn);
        collectionBtn.setOnClickListener(this);
        rootView.findViewById(R.id.cancelBtn).setOnClickListener(this);
        setContentView(rootView);
        setOnDismissListener(new OnDismissListener() {
            @Override
            public void onDismiss() {
                WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
                lp.alpha = 1.0f;
                mContext.getWindow().setAttributes(lp);
            }
        });
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.collectionBtn://收藏
                dismiss();
                if (onMoreListener != null) {
                    onMoreListener.onCollection();
                }
                break;
            case R.id.cancelBtn://查看历史得分
                dismiss();
                break;

        }
    }

    public void setCollectionType(int type) {
        collectionBtn.setVisibility(View.VISIBLE);
        if (type == ExerciseBean.TYPE_UN_COLLECTION) {
            collectionBtn.setText(R.string.collect);
        } else if (type == ExerciseBean.TYPE_COLLECTION) {
            collectionBtn.setText(R.string.un_collection);
        } else {
            collectionBtn.setText(null);
            collectionBtn.setVisibility(View.GONE);
        }
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        super.showAtLocation(parent, gravity, x, y);
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = 0.4f;
        mContext.getWindow().setAttributes(lp);
    }

    public OnMoreListener getOnMoreListener() {
        return onMoreListener;
    }

    public void setOnMoreListener(OnMoreListener onMoreListener) {
        this.onMoreListener = onMoreListener;
    }

    private OnMoreListener onMoreListener;

    public interface OnMoreListener {
        void onCollection();
    }

}
